<?php
namespace Kuoqi\Model;

use \Kuoqi\Model\Model\FilterModel;//引入字段过滤器
/**
 * Class ValidataModel
 * @package Kuoqi\Model\Model
 * User kuoqi
 * Datetime 2022/3/4 16:52
 * Remark 对数据进行验证的模型
 */
class Validata{

    /**
     * @var array
     * User kuoqi
     * Datetime 2022/3/4 17:01
     * Remark 配置场景信息,配置每个场景需要验证的字段信息
     */
    protected $scale = array(
        'add'=>'',//默认增加数据的场景
        'edit'=>'',//默认编辑的场景
        'del'=>'',//默认删除的场景
    );

    /**
     * @var array
     * User kuoqi
     * Datetime 2022/3/4 17:26
     * Remark 字段信息设计
     */
    protected $message = array(
        'id'=>'索引为必填',
        'status'=>'状态信息为必填',
    );

    /**
     * @var array
     * User kuoqi
     * Datetime 2022/3/4 17:26
     * Remark 条件信息设计
     */
    protected $condition = array(
        'id'=>'require',
        'status'=>'intval',
    );

    /**
     * @var bool
     * User kuoqi
     * Datetime 2022/3/4 17:26
     * Remark 判定是否审核通过
     */
    protected $is_valied = false;

    /**
     * @var array
     * User kuoqi
     * Datetime 2022/3/4 17:26
     * Remark 待核验数据信息
     */
    protected $datas = array();

    /**
     * @var string
     * User kuoqi
     * Datetime 2022/3/4 17:27
     * Remark 获取异常信息
     */
    protected $errmsg = '';

    /**
     * @var string
     * User kuoqi
     * Datetime 2022/3/14 11:46
     * Remark 当前使用场景信息
     */
    protected $scale_now = '';

    //验证数据信息的操作
    public function check(){
        //执行数据验证
        if($this->is_valied){
            return true;
        }
        if($this->scale_now&&$this->scale&&isset($this->scale[$this->scale_now])&&$this->scale[$this->scale_now]){
            $scales = explode(',',$this->scale[$this->scale_now]);
        }else{
            //场景信息不存在，直接返回成功
            $this->is_valied = false;
            $this->errmsg = sprintf('验证失败：验证场景[%s]不存在',$this->scale_now);
            return true;
        }
        foreach($scales as $field){
            //$field ;; 字段信息
            //$condition 条件信息
            if(isset($this->condition[$field])){
                $condition = $this->condition[$field];
            }else{
                //字段信息不存在的情况下，跳过这个字段的检测
                new \Kuoqi\Error('请完善字段['.$field.']的条件信息');
            }
            $fieldval = isset($this->datas[$field])?$this->datas[$field]:null;
            if(!$fieldval){
                //字段信息不存在，直接返回
                $errmsg = isset($this->message[$field])?$this->message[$field]:'验证失败.字段['.$field.']不存在';
                $this->errmsg = $errmsg;
                $this->is_valied = false;
                return false;
            }
            if(is_object($fieldval)){
                //为一个对象,配合模型模块进行数据整理
                $rs = FilterModel::Filter($condition,$fieldval->getValue());
                if($rs===null){
                    //这个判定为验证失败，则直接返回字符串信息
                    $errmsg = isset($this->message[$field])?$this->message[$field]:'验证失败.字段['.$field.']不存在';
                    $this->errmsg = $errmsg;
                    $this->is_valied = false;
                    return false;
                }
                //判定成功，将数据重置并写入到数据中
                $fieldval->setValue($rs);
            }else{
                //判定为字段
                $rs = FilterModel::Filter($condition,$fieldval);
                if($rs===null){
                    //这个判定为验证失败，则直接返回字符串信息
                    $errmsg = isset($this->message[$field])?$this->message[$field]:'验证失败.字段['.$field.']不存在';
                    $this->errmsg = $errmsg;
                    $this->is_valied = false;
                    return false;
                }
            }
        }
        $this->is_valied = true;
        return true;
    }

    /**
     * @param string $scale
     * @return $this
     * User kuoqi
     * Datetime 2022/3/4 17:25
     * Remark 数据配置场景值
     */
    public function setScale($scale = ''){
        $this->scale_now = $scale;
        return $this;
    }

    /**
     * @param $scales
     * @return $this
     * User kuoqi
     * Datetime 2022/3/4 17:18
     * Remark 配置场景信息,动态绑定场景数据
     */
    public function bindScale($scales){
        $this->scale = $scales;
        return $this;
    }

    /**
     * @param $data
     * @return $this
     * User kuoqi
     * Datetime 2022/3/4 17:23
     * Remark 配置数据信息
     */
    public function setData($data){
        if(is_array($data)){
            $this->datas = $data;
        }
        return $this;
    }

    /**
     * @param $cdtions
     * @return $this
     * User kuoqi
     * Datetime 2022/3/4 17:23
     * Remark 配置条件信息
     */
    public function setCondition($cdtions){
        $this->condition = $cdtions;
        return $this;
    }

    /**
     * @param $fields
     * @return $this
     * User kuoqi
     * Datetime 2022/3/4 17:29
     * Remark 配置引出信息
     */
    public function setMessage($fields){
        $this->message = $fields;
        return $this;
    }

    /**
     * @return bool
     * User kuoqi
     * Datetime 2022/3/4 17:26
     * Remark 验证是否通过
     */
    public function isValied(){
        return $this->is_valied;
    }

    /**
     * @return mixed
     * User kuoqi
     * Datetime 2022/3/4 17:27
     * Remark 获取异常数据信息
     */
    public function getError(){
        return $this->errmsg;
    }

}